package org.file5.dao.impl;



import org.apache.log4j.Logger;
import org.file5.dao.AttachmentDao;
import org.file5.domain.Attachment;
import org.file5.util.DataBean;
import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.QueryResultIterator;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

/**
 * Operates on SwagImages. Inspired by Spring Image Database sample application
 */
@Repository("attachmentDao")
public class AttachmentDaoImpl implements AttachmentDao {

	private final Logger logger = Logger.getLogger(this.getClass());


	@Override
	public DataBean<Attachment> getAttachments(int startPosition,
			int maxResult, Object... params) {
		DataBean<Attachment> dataBean = new DataBean<Attachment>();

		Objectify ofy = ObjectifyService.begin();
		Query<Attachment> query = ofy.query(Attachment.class);

		String _fileType = null;
		if (params != null) {
			for (Object o : params) {
				if (o instanceof String) {
					_fileType = (String) o;break;
				}
			}
			if(_fileType!=null&&!"".equals(_fileType)){
			query.filter("fileType", _fileType);
			}
		}

		dataBean.setCount(query.countAll());
		query.offset(startPosition).limit(maxResult);

		QueryResultIterator<Attachment> iterator = query.iterator();
		while (iterator.hasNext()) {
			dataBean.addDataE(iterator.next());
		}
		return dataBean;

	}

}
